package com.example.userserver.utils;


import org.apache.commons.codec.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.UUID;

public class Md5Utils {

  public static final Logger logger = LoggerFactory.getLogger(Md5Utils.class);

  public static String generateMd5(String str) {
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(str.getBytes(Charsets.UTF_8));
      return new BigInteger(1, md.digest()).toString(16);
    } catch (Exception e) {
      logger.error("md5 str: {} failed, exception is :{}", str, e);
    }
    return null;
  }

  public static String getRandom32Str() {
    UUID uuid = UUID.randomUUID();
    return uuid.toString().replaceAll("-", "");
  }

}
